package com.adams.custom.page.mgt.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.adams.access.db.bean.ViewEvent;
import com.adams.access.db.bean.ViewPanel;
import com.adams.access.db.bean.ViewWin;
import com.adams.core.exception.BusinessException;
import com.adams.custom.page.mgt.dao.impl.ViewPanelDaoImpl;
import com.adams.custom.page.mgt.dao.impl.ViewWinDaoImpl;
import com.adams.custom.page.mgt.service.ViewRoleEventService;

@Service("viewRoleEventService")
@Transactional
public class ViewRoleEventServiceImpl implements ViewRoleEventService {

    @Autowired
    private ViewWinDaoImpl viewWinDaoImpl;
    @Autowired
    private ViewPanelDaoImpl viewPanelDaoImpl;

    @Override
    public List<ViewEvent> getEventsByMeunId(int menuId) throws BusinessException {
        List<ViewEvent> events = new ArrayList<ViewEvent>();
        List<ViewWin> result = viewWinDaoImpl.findByMenuid(new Long(menuId));
        if (result == null) {
            return events;
        } else {
            if (!result.isEmpty()) {
                ViewWin win = null;
                for (ViewWin winR : result) {
                    if ("dynamicGrid-win".equals(winR.getXtype())) {
                        win = winR;
                        break;
                    }
                }
                if (win != null) {
                    // 查询panel
                    List<ViewPanel> panels = viewPanelDaoImpl.findByViewwinid(win.getId());
                    if (panels == null || panels.isEmpty()) {
                        return events;
                    } else {
                        ViewPanel panel = panels.get(0);
                        events = viewPanelDaoImpl.findEvents(panel.getId());
                    }
                }
            }
        }

        return events;
    }

}
